package runnerworm.core;

import java.awt.*;

/**
 * <p>Title: AvailableEventLauncher</p>
 * <p>Description: It has only one meaning - launch AvailableEvents</p>
 * @author Guilherme Mauro Germoglio Barbosa
 */

public class AvailableEventLauncher
    extends Component {

  /**
   * This is the event queue.
   */
  private static EventQueue eventQueue;
  /**
   * This will be the Enginelistener
   */
  private EngineListener listener;

  /**
   * The constructor. It will enable the events launching.
   */
  public AvailableEventLauncher() {
    this.eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
    enableEvents(0);
  }

  /**
   * Adds a listener to this class' events.
   * @param l The EngineListener listener.
   */
  public void addEngineListener(EngineListener l) {
    listener = l;
  }

  /**
   * Maybe puts the event on the event queue, or something like that.
   * @param evt The event will be put on the queue
   */
  public void processEvent(AWTEvent evt) {
    if (evt instanceof AvailableEvent) {
      if (listener != null)
        switch(evt.getID()) {
          case AvailableEvent.ABOUT_AVAILABLE:
            listener.gotAboutAvailable(((AboutAvailableEvent)evt).getAboutText());
            break;
          case AvailableEvent.HELP_AVAILABLE:
            listener.gotHelpAvailable(((HelpAvailableEvent)evt).getHelpText());
            break;
          case AvailableEvent.HIGH_SCORES_AVAILABLE:
            listener.gotHighScoresAvailable(((HighScoresAvailableEvent)evt).getHighScores());
            break;
          case AvailableEvent.OPTIONS_AVAILABLE:
            listener.gotOptionsAvailable(((OptionsAvailableEvent)evt).getGameOptions());
            break;
          case AvailableEvent.EXCEPTION_AVAILABLE:
            listener.gotExceptionAvailable(((ExceptionAvailableEvent)evt).getException());
            break;
          case AvailableEvent.ASK_FOR_PLAYER_NAME:
            listener.askPlayerName();
            break;
        }
    }
    else
      super.processEvent(evt);
  }

  /**
   * Creates and launches a AboutAvailableEvent.
   * @param aboutText The text will be shown.
   */
  public void createAboutAvailableEvent(String aboutText) {
    eventQueue.postEvent(new AboutAvailableEvent(this, aboutText));
  }

  /**
   * Creates and launches a OptionsAvailableEvent.
   * @param gameOptions The current game options.
   */
  public void createOptionsAvailableEvent(GameOptions gameOptions) {
    eventQueue.postEvent(new OptionsAvailableEvent(this, gameOptions));
  }

  /**
   * Creates and launches a HighScoresAvailableEvent.
   * @param highScores The game's high scores.
   */
  public void createHighScoresAvailableEvent(HighScores highScores) {
    eventQueue.postEvent(new HighScoresAvailableEvent(this, highScores));
  }


  /**
   * Creates and launches a HelpAvailableEvent.
   * @param helpText The text will be shown.
   */
  public void createHelpAvailableEvent(String helpText) {
    eventQueue.postEvent(new HelpAvailableEvent(this, helpText));
  }

  /**
   * Creates the launches an ExceptionAvailableEvent.
   * @param e The exception caught.
   */
  public void createExceptionAvailableEvent(Exception e) {
    eventQueue.postEvent(new ExceptionAvailableEvent(this, e));
  }

  /**
   * Creates and launches an AskForPlayerNameEvent.
   */
  public void createAskForPlayerNameEvent() {
    eventQueue.postEvent(new AskForPlayerNameEvent(this));
  }

}